package cn.iocoder.yudao.module.worker.dal.mysql.clerklevel;

import java.util.*;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.worker.controller.app.clerklevel.vo.AppClerkLevelPageReqVO;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerklevel.ClerkLevelDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.worker.controller.admin.clerklevel.vo.*;
import org.springframework.util.CollectionUtils;

/**
 * 店员等级 Mapper
 *
 * @author Jerry
 */
@Mapper
public interface ClerkLevelMapper extends BaseMapperX<ClerkLevelDO> {

    default PageResult<ClerkLevelDO> selectPage(ClerkLevelPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ClerkLevelDO>()
                .eqIfPresent(ClerkLevelDO::getLevel, reqVO.getLevel())
                .likeIfPresent(ClerkLevelDO::getName, reqVO.getName())
                .eqIfPresent(ClerkLevelDO::getImg, reqVO.getImg())
                .eqIfPresent(ClerkLevelDO::getMoney, reqVO.getMoney())
                .eqIfPresent(ClerkLevelDO::getGoodsList, reqVO.getGoodsList())
                .eqIfPresent(ClerkLevelDO::getWeight, reqVO.getWeight())
                .betweenIfPresent(ClerkLevelDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ClerkLevelDO::getWeight));
    }

    default PageResult<ClerkLevelDO> selectPage(AppClerkLevelPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ClerkLevelDO>()
                .eqIfPresent(ClerkLevelDO::getLevel, reqVO.getLevel())
                .likeIfPresent(ClerkLevelDO::getName, reqVO.getName())
                .eqIfPresent(ClerkLevelDO::getImg, reqVO.getImg())
                .eqIfPresent(ClerkLevelDO::getMoney, reqVO.getMoney())
                .eqIfPresent(ClerkLevelDO::getGoodsList, reqVO.getGoodsList())
                .eqIfPresent(ClerkLevelDO::getWeight, reqVO.getWeight())
                .betweenIfPresent(ClerkLevelDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ClerkLevelDO::getWeight));
    }

    default ClerkLevelDO getClerkLevel() {
        AppClerkLevelPageReqVO reqVO = new AppClerkLevelPageReqVO();
        List<ClerkLevelDO> list = selectList(new LambdaQueryWrapperX<ClerkLevelDO>()
                .eqIfPresent(ClerkLevelDO::getLevel, reqVO.getLevel())
                .likeIfPresent(ClerkLevelDO::getName, reqVO.getName())
                .eqIfPresent(ClerkLevelDO::getImg, reqVO.getImg())
                .eqIfPresent(ClerkLevelDO::getMoney, reqVO.getMoney())
                .eqIfPresent(ClerkLevelDO::getGoodsList, reqVO.getGoodsList())
                .eqIfPresent(ClerkLevelDO::getWeight, reqVO.getWeight())
                .eq(ClerkLevelDO::getStatus, true)
                .betweenIfPresent(ClerkLevelDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(ClerkLevelDO::getMoney));
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    default ClerkLevelDO getAppClerkLevel(){
        AppClerkLevelPageReqVO reqVO = new AppClerkLevelPageReqVO();
        List<ClerkLevelDO> list = selectList(new LambdaQueryWrapperX<ClerkLevelDO>()
                .eqIfPresent(ClerkLevelDO::getLevel, reqVO.getLevel())
                .likeIfPresent(ClerkLevelDO::getName, reqVO.getName())
                .eqIfPresent(ClerkLevelDO::getImg, reqVO.getImg())
                .eqIfPresent(ClerkLevelDO::getMoney, reqVO.getMoney())
                .eqIfPresent(ClerkLevelDO::getGoodsList, reqVO.getGoodsList())
                .eqIfPresent(ClerkLevelDO::getWeight, reqVO.getWeight())
                .eq(ClerkLevelDO::getStatus, true)
                .betweenIfPresent(ClerkLevelDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(ClerkLevelDO::getMoney));
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    default ClerkLevelDO getClerkLevelByPrice(Integer price){
        return selectOne(new LambdaQueryWrapperX<ClerkLevelDO>()
                .eq(ClerkLevelDO::getStatus, true)
                .ge(ClerkLevelDO::getMoney, price)
                .orderByAsc(ClerkLevelDO::getMoney)
                .last("LIMIT 1"));
    }
}